package com.yjt.fresh.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.MainActivity;
import com.yjt.fresh.MyApp;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpRvAtyProducts;
import com.yjt.fresh.api.ApiGetProducts;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanProduct;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.CommonsToolsHelper;
import com.yjt.fresh.widget.GridSpacingItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018-08-16.
 */

public class AtyPayResult extends BaseSimpleActivity {
    private static final int PAGE_SIZE = 10;
    @BindView(R.id.back)
    RelativeLayout mBack;
    @BindView(R.id.tvAppAtyPayResultSuccessOrFail)
    TextView mTvSuccessOrFail;
    @BindView(R.id.tvAppAtyPayResultCheckOrder)
    TextView mTvCheckOrder;
    @BindView(R.id.tvAppAtyPayResultContinue)
    TextView mTvContinue;
    @BindView(R.id.rvAppAtyPayResult)
    RecyclerView mRv;
    @BindView(R.id.srlAppAtyPayResult)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.tvAppAtyPayResultRePay)
    TextView mTvRePay;
    @BindView(R.id.ivAppAtyPayResultSuccessOrFail)
    ImageView mIvResult;
    private AdpRvAtyProducts mAdpProducts;
    private int mNextRequestPage = 1;
    private boolean isLoadMore;
    private boolean isShowProgress;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_pay_result;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        String payResult = getIntent().getStringExtra(MyConstants.PAY_RESULT);
        if (MyConstants.SUCCESS.equals(payResult)){
            mTvCheckOrder.setVisibility(View.VISIBLE);
            mTvContinue.setVisibility(View.VISIBLE);
            mTvRePay.setVisibility(View.GONE);
            mTvSuccessOrFail.setText("支付成功");
            mIvResult.setImageResource(R.mipmap.e1_9_ic_ok);

        }else if (MyConstants.FAIL.equals(payResult)){
            mTvSuccessOrFail.setText("支付失败");
            mTvRePay.setVisibility(View.VISIBLE);
            mTvCheckOrder.setVisibility(View.GONE);
            mTvContinue.setVisibility(View.GONE);
            mIvResult.setImageResource(R.mipmap.e1_9_ic_cuo);
        }
    }

    @Override
    protected void initEvent() {
        initRv();
        initRefresh();
        getData();
        initClick();
    }

    private void initClick() {
        mTvContinue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, MainActivity.class);
                startActivity(intent);
            }
        });

        mTvCheckOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,AtyMyOrder.class);
                intent.putExtra(MyConstants.POSITION,-1);//-1是进入订单的全部部分
                startActivity(intent);
                finish();
            }
        });

        mTvRePay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mAdpProducts.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mContext,AtyProductDetail.class);
                intent.putExtra(MyConstants.GOODS_ID, mAdpProducts.getItem(position).getGOODS_ID());
                startActivity(intent);
                List<Activity> activities = MyApp.getmActivityList();
                if (activities != null && activities.size() > 0){
                    for (int i = 0; i < activities.size(); i++) {
                        activities.get(i).finish();
                    }
                }
                finish();
            }
        });

        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initRv() {
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 2);
        List<BeanProduct> list = new ArrayList<>();
        mRv.setLayoutManager(layoutManager);
        mAdpProducts = new AdpRvAtyProducts(R.layout.app_recycle_item_products, list);
        mRv.addItemDecoration(new GridSpacingItemDecoration(2, CommonsToolsHelper.dip2px(mContext, 5), true));
        mRv.setAdapter(mAdpProducts);
    }

    private void initRefresh() {
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mNextRequestPage = 1;
                isLoadMore = false;
                isShowProgress = false;
                getData();
            }
        });
        mAdpProducts.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                isLoadMore = true;
                getData();
            }
        }, mRv);
        mAdpProducts.setEmptyView(R.layout.view_empty);
    }

    HttpOnNextListener<List<BeanProduct>> mProductsListener = new HttpOnNextListener<List<BeanProduct>>() {
        @Override
        public void onNext(List<BeanProduct> beanProducts) {
            isShowProgress = true;
            if (isLoadMore) {
                setData(false, beanProducts);
            } else {
                setData(true, beanProducts);
                mRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };
    private void setData(boolean isRefresh, List<BeanProduct> data) {
        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdpProducts.setNewData(data);
        } else {
            if (size > 0) {
                mAdpProducts.addData(data);
                mAdpProducts.loadMoreComplete();
            }else {
                mAdpProducts.loadMoreEnd(false);
            }
        }
    }
    private void getData() {
        Map<String, String> map = new HashMap<>();
        map.put("currentPage", mNextRequestPage+"");
        map.put("IS_INDEX", 1+"");
        map.put("GOODSHOWPLACE",1+"");
        ApiGetProducts apiGetProducts = new ApiGetProducts(mProductsListener, mContext, isShowProgress, false, true);
        apiGetProducts.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetProducts);
    }
}
